<?php
/**
 * Created by PhpStorm.
 * User: wanghaibo
 * Date: 17/11/14
 * Time: 11:14
 */

namespace App\Http\Controllers\Admin\Wechat\Send;


use App\Http\Controllers\Controller;
use App\Post;
use App\Service\Category;
use App\Service\WeChat;
use App\Service\Wechat\Send;
use Illuminate\Http\Request;

class IndexController extends Controller
{

    private $sendManager;
    private $postManager;
    private $request;
    private $wechatManager;


    public function __construct (Request $request)
    {
        $this->sendManager = new Send();
        $this->postManager = new Post();
        $this->wechatManager = new WeChat();
        $this->request = $request;
    }

    public function index(){
        return view('admin.wechat.send.index');
    }
    public function indexAjax(){
        return [
            'data'  =>  [
                [
                    'title'=>'标题1',
                ],
                [
                    'title' =>  '标题2'
                ]
            ],
            'pages' =>  4
        ];
    }

    public function add(){
        $id = $this->request->get('id',0);
        $post = $this->postManager->getPost($id);
        if(!$post)$id = 0;
        return view('admin.wechat.send.add',[
            'id'=>$id,
            'post'=>$post
        ]);
    }

    public function store(){
        $this->wechatManager->uploadArticle($this->request->post('ids'));
        return redirect()->route('admin.wechat.send');
    }

    public function send(){}

    public function newsList(){
        return view('admin.wechat.send.news-list',[
            'status' =>    $this->postManager->getStatusList(),
            'recommend' =>  $this->postManager->getRecommendedList(),
            'categories'    =>  getTree(Category::all()->toArray()),
            'postManager'   =>  $this->postManager
        ]);
    }

    public function newsListAjax(){
        return $this->page_json_render($this->postManager->getPageList($this->request));

    }
}